/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.mail.test.base;

import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.common.service.base.test.RepositoryTestContext;
import info.peigen.hotpot.common.service.base.test.RepositoryTestInvoke;
import info.peigen.hotpot.component.mail.domain.MailTemplate;
import info.peigen.hotpot.component.mail.repository.entity.MailTemplateEntity;
import info.peigen.hotpot.component.mail.repository.service.MailTemplateRepository;
import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;
import org.junit.jupiter.api.Assertions;
import org.noear.solon.annotation.Inject;


/**
 * <b>(MailRepositoryTestBase)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023/1/23
 */
@FieldDefaults(level = AccessLevel.PROTECTED)
public class MailRepositoryTestBase extends MailTestBase {
    @Inject
    MailTemplateRepository mailTemplateRepository;

    public String genNewMailTemplate(String templateId) {
        RepositoryTestContext repositoryTestContext = RepositoryTestContext.builder().build();

        executeRepositoryTest(repositoryTestContext, new RepositoryTestInvoke() {
            @Override
            public void deleteData(RepositoryTestContext testContext) {
                if (StrUtil.isNotEmpty(templateId)) {
                    mailTemplateRepository.getMapper().deleteByTemplateId(templateId);
                }
            }

            @Override
            public void insertData(RepositoryTestContext testContext) {
                MailTemplate mailTemplate = genMailTemplate(templateId);
                mailTemplateRepository.store(mailTemplate);
                testContext.getExtend().put("templateId", mailTemplate.getTemplateId());
            }

            @Override
            public void insertAssert(RepositoryTestContext testContext) {
                String             templateId = (String) testContext.getExtend().get("templateId");
                MailTemplateEntity entity     = mailTemplateRepository.getMapper().findByTemplateId(templateId);
                testContext.setEntity(entity);
                Assertions.assertNotNull(entity);
            }
        });

        return (String) repositoryTestContext.getExtend().get("templateId");
    }

    public String genNewInvoiceMailTemplate(String templateId) {
        RepositoryTestContext repositoryTestContext = RepositoryTestContext.builder().build();

        executeRepositoryTest(repositoryTestContext, new RepositoryTestInvoke() {
            @Override
            public void deleteData(RepositoryTestContext testContext) {
                if (StrUtil.isNotEmpty(templateId)) {
                    mailTemplateRepository.getMapper().deleteByTemplateId(templateId);
                }
            }

            @Override
            public void insertData(RepositoryTestContext testContext) {
                MailTemplate mailTemplate = genInvoiceMailTemplate(templateId);
                mailTemplateRepository.store(mailTemplate);
                testContext.getExtend().put("templateId", mailTemplate.getTemplateId());
            }

            @Override
            public void insertAssert(RepositoryTestContext testContext) {
                String             templateId = (String) testContext.getExtend().get("templateId");
                MailTemplateEntity entity     = mailTemplateRepository.getMapper().findByTemplateId(templateId);
                testContext.setEntity(entity);
                Assertions.assertNotNull(entity);
            }
        });

        return (String) repositoryTestContext.getExtend().get("templateId");
    }
}